package com.cn.redis.client;

import com.cn.redis.exception.RedisConnectException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("com.cn.redis.client.ServerNodeManager")
public class a {
    @Resource(
            name = "com.cn.redis.client.RedisClient"
    )
    private RedisClient C;
    @Resource(
            name = "com.cn.redis.idgenerator.redis.BackClient"
    )
    private BackClient D;
    private static final String _key = ".back";

    public a() {
    }

    public long a(String seqName, int step) {
        long max = 0L;
        long max_back = 0L;
        max = this.C.incrBy(seqName, step);
        if (max < 1L) {
            throw new RedisConnectException("redis获取步长失败");
        } else {
            for(max_back = this.D.get(seqName + ".back");
                max < max_back + (long)step;
                max = this.C.incrBy(seqName, step)) {
            }

            this.D.set(seqName + ".back", max);
            return max;
        }
    }

    public void init(String seqName) {
        this.C.init();
    }
}
